package com.ziyi.tiantianshuiyin.base;

import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;


import com.gtdev5.geetolsdk.mylibrary.initialization.GeetolSDK;
import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.ziyi.tiantianshuiyin.util.AppConfig;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BaseApplication extends MultiDexApplication {

    public static Context mContext;
    private static BaseApplication appInstance;
    public static IWXAPI mWXApi;
    public static int identity = -1;//微信登录  1 登录 2 绑定

    public static BaseApplication getInstance() {
        if (null == appInstance) {
            appInstance = new BaseApplication();
        }
        return appInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appInstance = this;
        //sdk http://shashidi.weiapp8.cn/app/
        GeetolSDK.init(this, "http://ziyi.ziyi18.com/app/");
        //禁止小米弹窗
        disableAPIDialog();
        UMConfigure.preInit(this, AppConfig.UMENG_KEY, "geetol");
        //判断是否同意了服务协议
        if (SpUtils.getInstance().getBoolean(AppConfig.IS_CONFIRM_USER_AGREEMENT)) {
            //友盟统计
            UMConfigure.init(this, AppConfig.UMENG_KEY, "geetol", UMConfigure.DEVICE_TYPE_PHONE, "");
            // 选用AUTO页面采集模式
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        }
        registerToWX();
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }

    }

    /**
     * 微信注册
     */
    private void registerToWX() {
        mWXApi = WXAPIFactory.createWXAPI(this, AppConfig.WX_APP_ID, false);
        mWXApi.registerApp(AppConfig.WX_APP_ID);
    }

    /**
     * application里面 调用  反射 禁止弹窗 解决 detected problems with api  9.0手机（小米）
     */
    private void disableAPIDialog() {
        try {
            Class clazz = Class.forName("android.app.ActivityThread");
            Method currentActivityThread = clazz.getDeclaredMethod("currentActivityThread");
            currentActivityThread.setAccessible(true);
            Object activityThread = currentActivityThread.invoke(null);
            Field mHiddenApiWarningShown = clazz.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
